package top.taven.http;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class HttpClientDemo {

    public static void main(String[] args) {

        try{

            Demo1();

        }catch (Exception e){
            e.printStackTrace();
        }


    }

    public static void Demo1() throws Exception {
        HttpClient client = HttpClient.newHttpClient();

        HttpRequest request = HttpRequest.newBuilder()
                .uri(new URI("https://4.ipw.cn")).timeout(Duration.ofMillis(3000))
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body());
    }

    public static void Demo2() throws Exception {

        String url = "https://4.ipw.cn";

        String payload = """
                GET / HTTP/1.1
                Host: 4.ipw.cn
                Cookie: Hm_lvt_c4be47aebad452af3f721ea14b84e41d=1710906884,1711017976,1713433429; Hm_lpvt_c4be47aebad452af3f721ea14b84e41d=1713433429
                Sec-Ch-Ua: "Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"
                Sec-Ch-Ua-Mobile: ?0
                Sec-Ch-Ua-Platform: "Windows"
                Upgrade-Insecure-Requests: 1
                User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
                Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
                Sec-Fetch-Site: none
                Sec-Fetch-Mode: navigate
                Sec-Fetch-User: ?1
                Sec-Fetch-Dest: document
                Accept-Encoding: gzip, deflate, br
                Accept-Language: zh-CN,zh;q=0.9
                Connection: close
                """;

        HttpClient client = HttpClient.newHttpClient();

        //HttpRequest.BodyPublishers.noBody()
        //HttpRequest.BodyPublishers.ofString("")
        //HttpRequest.BodyPublishers.ofByteArray(new byte[] { ... })
        //HttpRequest.BodyPublishers.ofFile(Paths.get("file.json"))

        HttpRequest httpRequest = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .POST(HttpRequest.BodyPublishers.noBody()).build();

        HttpRequest.newBuilder()
                .method("POST", HttpRequest.BodyPublishers.ofString(""))
                .uri(new URI(url))
                .header("Content-Type", "application/json; charset=UTF-8")
                .header("Accept", "application/json")
                .timeout(Duration.ofMillis(10000));



    }
}
